cat('Creating Tables A5 and A6 \n\n')

dat = read.csv('TablesA5A6_data.csv')

dat.non.south = dat[dat$south == 0,]

##regressions for tables
reg1 = lm(stereotypes~dissimilarity+percent.black,
             data = dat.non.south)
clustered.se = cl(dat.non.south, reg1, dat.non.south$cbsa_NAMELSAD10)
reg1$se = clustered.se

reg2 = lm(stereotypes~dissimilarity*percent.black,
             data = dat.non.south)
clustered.se = cl(dat.non.south, reg2, dat.non.south$cbsa_NAMELSAD10)
reg2$se = clustered.se

reg3 = lm(stereotypes~dissimilarity+percent.black+I(percent.black^2),
             data = dat.non.south)
clustered.se = cl(dat.non.south, reg3, dat.non.south$cbsa_NAMELSAD10)
reg3$se = clustered.se


reg4 = lm(stereotypes~dissimilarity*percent.black+dissimilarity*I(percent.black^2),
             data = dat.non.south)
clustered.se = cl(dat.non.south, reg4, dat.non.south$cbsa_NAMELSAD10)
reg4$se = clustered.se

reg5 = lm(stereotypes~dissimilarity+percent.black+income.recode+college.educated,
          data = dat.non.south)
clustered.se = cl(dat.non.south, reg5, dat.non.south$cbsa_NAMELSAD10)
reg5$se = clustered.se

reg6 = lm(stereotypes~dissimilarity*percent.black+income.recode+college.educated,
          data = dat.non.south)
clustered.se = cl(dat.non.south, reg6, dat.non.south$cbsa_NAMELSAD10)
reg6$se = clustered.se

reg7 = lm(stereotypes~dissimilarity+percent.black+I(percent.black^2)+income.recode+college.educated,
          data = dat.non.south)
clustered.se = cl(dat.non.south, reg7, dat.non.south$cbsa_NAMELSAD10)
reg7$se = clustered.se


reg8 = lm(stereotypes~dissimilarity*percent.black+dissimilarity*I(percent.black^2)+income.recode+college.educated,
          data = dat.non.south)
clustered.se = cl(dat.non.south, reg8, dat.non.south$cbsa_NAMELSAD10)
reg8$se = clustered.se


##print table
coef.names = c('Intercept', 'Segregation', 'Black Population',  'Segregation x Black Population', 'Black Population\verb|^|2','Segregation x Black Population\verb|^|2','Income','College Educated')

outtable = apsrtable(reg1,
                     reg2,
                     reg3,
                     reg4,
                     reg5,
                     reg6,
                     reg7,
                     reg8,
                     Sweave = T,
                     coef.names = coef.names,
                     #model.names = c('UO','Non UO','UO','Non UO','UO','Non UO'),
                     notes = '',
                     stars = 'default'
              )
writeLines(
  outtable, 'TableA6.tex')



##everybody
##regressions for tables
reg1 = lm(stereotypes~dissimilarity+percent.black,
          data = dat)
clustered.se = cl(dat, reg1, dat$cbsa_NAMELSAD10)
reg1$se = clustered.se

reg2 = lm(stereotypes~dissimilarity*percent.black,
          data = dat)
clustered.se = cl(dat, reg2, dat$cbsa_NAMELSAD10)
reg2$se = clustered.se

reg3 = lm(stereotypes~dissimilarity+percent.black+I(percent.black^2),
          data = dat)
clustered.se = cl(dat, reg3, dat$cbsa_NAMELSAD10)
reg3$se = clustered.se


reg4 = lm(stereotypes~dissimilarity*percent.black+dissimilarity*I(percent.black^2),
          data = dat)
clustered.se = cl(dat, reg4, dat$cbsa_NAMELSAD10)
reg4$se = clustered.se

reg5 = lm(stereotypes~dissimilarity+percent.black+income.recode+college.educated,
          data = dat)
clustered.se = cl(dat, reg5, dat$cbsa_NAMELSAD10)
reg5$se = clustered.se

reg6 = lm(stereotypes~dissimilarity*percent.black+income.recode+college.educated,
          data = dat)
clustered.se = cl(dat, reg6, dat$cbsa_NAMELSAD10)
reg6$se = clustered.se

reg7 = lm(stereotypes~dissimilarity+percent.black+I(percent.black^2)+income.recode+college.educated,
          data = dat)
clustered.se = cl(dat, reg7, dat$cbsa_NAMELSAD10)
reg7$se = clustered.se


reg8 = lm(stereotypes~dissimilarity*percent.black+dissimilarity*I(percent.black^2)+income.recode+college.educated,
          data = dat)
clustered.se = cl(dat, reg8, dat$cbsa_NAMELSAD10)
reg8$se = clustered.se


##print table
coef.names = c('Intercept', 'Segregation', 'Black Population',  'Segregation x Black Population', 'Black Population\verb|^|2','Segregation x Black Population\verb|^|2','Income','College Educated')

outtable = apsrtable(reg1,
                     reg2,
                     reg3,
                     reg4,
                     reg5,
                     reg6,
                     reg7,
                     reg8,
                     Sweave = T,
                     coef.names = coef.names,
                     #model.names = c('UO','Non UO','UO','Non UO','UO','Non UO'),
                     notes = '',
                     stars = 'default'

)
writeLines(
  outtable, 'TableA5.tex')


